<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .box{
        width: 80px;
        height: 80px;
        border: 1px solid #ccc;
        position: relative;
        background-color: #999;
        line-height: 80px;
        text-align: center;
        font-size: 30px;
        border-radius: 5px;
        font-weight: bold;
        color: white;
        margin: 10px auto;
        input{
            width: 100%;
            height: 100%;
            position: absolute;
            top: 0;
            left: 0;
            opacity: 0;
        }
    }
</style>
<body>
    <div class="box">+
        <input type="file" multiple>
    </div>
    
    <script>

        const inp = document.querySelector('input')
        let imgRes = /^image\//
        let textRes = /^text/
        inp.addEventListener('change',e =>{
            let files = Object.entries(e.target.files)
            console.log(files,Array.isArray(files))
            files.forEach(item => {
                console.log(item[1].type)
                const type = item[1].type
                if(!imgRes.test(type) && !textRes.test(type)){
                    alert('文件格式错误')
                }else{
                    // 读取文件
                    const fs = new FileReader()
                    if(imgRes.test(type)){
                        // 读取文件内容，转换成base64格式
                        fs.readAsDataURL(item[1])
                        console.log(fs)
                        fs.onload = () => {
                            const img = new Image()
                            img.width = 500
                            img.src = fs.result
                            img.onload = () => {
                                document.body.appendChild(img)
                            }
                        }  
                    }else{
                        // 读取文件内容，转换成base64格式
                        fs.readAsText(item[1])
                        fs.onload = () => {
                            const box = document.createElement('div')
                            box.style.cssText = `
                                border: 1px solid red;
                                width: 80%;
                                height: 200px;
                                overflow: auto;
                                margin:0 auto 10px;
                            `
                            box.innerText = fs.result
                            document.body.appendChild(box)
                        }

                    }
                    console.log('文件格式正确')
                }
            })
        })
    </script>
</body>
</html>